<?php
/**
 *    前台控制器基础类
 *
 *    @author    Garbin
 *    @usage    none
 */
class BackendApp extends ECBaseApp
{
    function __construct()
    {
        $this->BackendApp();
    }
    function BackendApp()
    {
		Lang::load(lang_file('common'));
        Lang::load(lang_file(APP));
        parent::__construct();

        // 判断商城是否关闭
        if (!Conf::get('site_status'))
        {
            $this->show_warning(Conf::get('closed_reason'));
            exit;
        }
        # 在运行action之前，无法访问到visitor对象
    }

    function _config_view()
    {
        parent::_config_view();
        $this->_view->template_dir  = APP_ROOT . '/templates';
        $this->_view->compile_dir   = ROOT_PATH . '/temp/compiled/wap';
        $this->_view->res_base      = site_url() . '/templates';
        $this->_view->lib_base      = dirname(site_url()) . '/includes/libraries/javascript';
        $this->_config_seo(array(
            'title' => Conf::get('site_name'),
            'description' => Conf::get('site_description'),
            'keywords' => Conf::get('site_keywords')
        ));		
    }	
    function display($tpl)
    {
		$this->assign('wap_site', site_url());//获得网站的URL地址
		$this->assign('site_title', Conf::get('site_title'));
		$this->assign('site_name', Conf::get('site_name'));
		$this->assign('subdomain_suffix', SUBDOMAIN_SUFFIX);
		$this->assign('config', array( 
			'service_tel'=>Conf::get('service_tel'),//客服电话
			'icp_number'=>Conf::get('icp_number'),//备案号
			'rewrite_enabled'=>Conf::get('rewrite_enabled'),//是否伪静态
			'statistics_code'=>Conf::get('statistics_code'), //统计代码
		));		

        parent::display($tpl);
	}
	//添加操作日志 by liangcaijian
	function write_log($user_id, $user_name, $log_value, $type = 0, $lexing = 0)
	{
		if($user_id and $user_name and $log_value)
		{
		  //取得本月的第几个星期，一个星期打一个日志
		  $date_now=date("j"); //得到几号
		  $cal_result = "_" . ceil($date_now/7). "week"; //计算是第几个星期几
		  $filename = ROOT_PATH . "/temp/log_web/" .date("Y_m"). $cal_result . ".log";//date("Y_m")
		  if (!is_dir('temp/log_web'))
		  {
			  ecm_mkdir(ROOT_PATH . '/' . 'temp/log_web');
		  }   
		  $handler = null;
		  if (($handler = fopen($filename, 'ab+')) !== false)
		  {
			  fwrite($handler, $type .'///'. $user_id .'///'. $user_name .'///'. real_ip() .'///'. date("Y-m-d H:i:s") .'///'. $log_value ."\n");
			  fclose($handler);
		  }		          
		}
	}
    function pop_warning ($msg, $dialog_id = '',$url = '')
    {
        if($msg == 'ok')
        {
            if(empty($dialog_id))
            {
                $dialog_id = APP . '_' . ACT;
            }
            if (!empty($url))
            {
                echo "<script type='text/javascript'>window.parent.location.href='".$url."';</script>";
            }
            echo "<script type='text/javascript'>window.parent.js_success('" . $dialog_id ."');</script>";
        }
        else
        {
            header("Content-Type:text/html;charset=".CHARSET);
            $msg = is_array($msg) ? $msg : array(array('msg' => $msg));
            $errors = '';
            foreach ($msg as $k => $v)
            {
                $error = $v[obj] ? Lang::get($v[msg]) . " [" . Lang::get($v[obj]) . "]" : Lang::get($v[msg]);
                $errors .= $errors ? "<br />" . $error : $error;
            }
            echo "<script type='text/javascript'>window.parent.js_fail('" . $errors . "');</script>";
        }
    }

    function logout()
    {
        $this->visitor->logout();

        /* 跳转到登录页，执行同步退出操作 */
        header("Location: index.php?app=member&act=login&synlogout=1");
        return;
    }


    /**
     *    获取JS语言项
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function jslang()
    {
        $lang = Lang::fetch(lang_file('jslang'));
        parent::jslang($lang);
    }


    /**
     *    获取当前使用的风格名称
     *
     *    @author    Garbin
     *    @return    string
     */
    function _get_style_name()
    {
        return 'default';
    }

    function _init_visitor()
    {
        $this->visitor =& env('visitor', new UserVisitor());
    }

}


/**
 *    用户中心子系统基础类
 *
 *    @author    Garbin
 *    @usage    none
 */
class MemberbaseApp extends BackendApp
{

    /**
     *    会员中心的需要权限验证机制
     *
     *    @author    Garbin
     *    @return    void
     */
    function _run_action()
    {
        /* 只有登录的用户才可访问 */
        if (!$this->visitor->has_login && !in_array(ACT, array('login', 'register', 'check_user')))
        {
            if (!IS_AJAX)
            {
                header('Location:index.php?app=member&act=login&ret_url=' . rawurlencode($_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']));
                return;
            }
            else
            {
                $this->json_error('login_please');
                return;
            }			
        }

        /* 运行 */
        parent::_run_action();
    }
}	
	
	/**
 *    购物流程子系统基础类
 *
 *    @author    Garbin
 *    @usage    none
 */
class ShoppingbaseApp extends BackendApp
{
    function _run_action()
    {
        /* 只有登录的用户才可访问 */
        if (!$this->visitor->has_login && !in_array(ACT, array('login', 'register', 'check_user')))
        {
            if (!IS_AJAX)
            {
                header('Location:index.php?app=member&act=login&ret_url=' . rawurlencode($_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']));

                return;
            }
            else
            {
                $this->json_error('请登录会员');
                return;
            }
        }

        parent::_run_action();
    }
}

/**
 *    前台访问者
 *
 *    @author    Garbin
 *    @usage    none
 */
class UserVisitor extends BaseVisitor
{
    var $_info_key = 'user_info';

    /**
     *    退出登录
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function logout()
    {
        /* 将购物车中的相关项的session_id置为空 */
        $mod_cart =& m('cart');
        $mod_cart->edit("user_id = '" . $this->get('user_id') . "'", array(
            'session_id' => '',
        ));

        /* 退出登录 */
        parent::logout();
    }
}


/* 实现消息基础类接口 */
class MessageBase extends BackendApp {};

/* 实现模块基础类接口 */
class BaseModule  extends BackendApp {};

/* 消息处理器 */
require(ROOT_PATH . '/eccore/controller/message.base.php');
?>